package com.cn.yugan.Utils.CommentListView;

import java.util.Map;


import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.cn.yugan.Engine.BitmapCache;
import com.cn.yugan.Engine.Json.CommentItemJsonObj;
import com.cn.yugan.Engine.Beans.CommentFloor;
import com.cn.yugan.Engine.Beans.CommentObj;
import com.cn.yugan.Utils.LogUtils;
import com.cn.yugan.Widget.Webview.JSInterface;
import com.cn.yugan.mobile.R;
import com.cn.yugan.Utils.MLog;
import com.cn.yugan.Utils.MyViews.MyCommentBgView;
import com.cn.yugan.Widget.CircularImage.CircularImage;


/**
 * 一个封装好的通用适配器类
 * <p/>
 * 需要配合分装好的通用holder类使用
 *
 * @param
 * @author ChengChao
 * @since 2015年3月11日 下午2:58:33
 */
public class CommentListViewAdapter extends BaseAdapter {
    protected Context mContext;
    private CommentObj commentObj;
    protected LayoutInflater mInflater;
    private Map<CommentFloor, OnClickListener> hideBtnOnclickMap;
    private RequestQueue mQueu;
    private ImageLoader imageLoader;

    public CommentListViewAdapter(Context context, CommentObj commentObj,
                                  Map<CommentFloor, OnClickListener> hideBtnOnclickMap) {
        this.mContext = context;
        this.mInflater = LayoutInflater.from(mContext);
        this.hideBtnOnclickMap = hideBtnOnclickMap;
        this.commentObj = commentObj;
        mQueu = Volley.newRequestQueue(context);
        imageLoader = new ImageLoader(mQueu, new BitmapCache());
        
    }

    @Override
    public int getCount() {
        return commentObj.getCurSize();
    }

    @Override
    public Object getItem(int position) {
        return commentObj.getItem(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        MLog.d("adapter getview " + position);
        final CommentItemJsonObj item = (CommentItemJsonObj) getItem(position);
        String url = mContext.getString(R.string.imgserverurl) + item.userface + "imageView2/1/w/75/h/75:";
        final CommentViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item_comment_bottom, parent,
                    false);
            holder = new CommentViewHolder();
            holder.tvNickname = (TextView) convertView
                    .findViewById(R.id.tv_comment_bottom_u_name);
            holder.tvNum = (TextView) convertView
                    .findViewById(R.id.tv_comment_bottom_num);
            holder.tvContent = (WebView) convertView
                    .findViewById(R.id.tv_comment_bottom_content);
            holder.tvContent.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
            holder.tvContent.setBackgroundColor(Color.alpha(0));
            holder.tvContent.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
            holder.tvContent.getSettings().setDomStorageEnabled(true);

            holder.tvContent.setWebChromeClient(new WebChromeClient() {
                @Override
                public void onProgressChanged(WebView view, int newProgress) {
                    view.getSettings().setJavaScriptEnabled(true);
                    super.onProgressChanged(view, newProgress);
                    if (newProgress == 100) {
                        holder.tvContent.loadUrl("javascript:(function(){" +
                                "var objs = document.getElementsByTagName(\"img\"); " +
                                "for(var i=0;i<objs.length;i++)  " +
                                "{"
                                + "    objs[i].onclick=function()  " +
                                "    {  "
                                + "        window.imggetter.openImage(this.src);  " +
                                "    }  " +
                                "}" +
                                "})()");
                    }
                }
            });
            holder.tvContent.getSettings().setJavaScriptEnabled(true);
            holder.tvContent.addJavascriptInterface(new JSInterface(mContext), "imggetter");
            holder.styleView = (MyCommentBgView) convertView.findViewById(R.id.bottom_style_view);

            holder.layoutContent = (LinearLayout) convertView
                    .findViewById(R.id.bottom_layout_content);
            holder.layoutCommentParent = (LinearLayout) convertView
                    .findViewById(R.id.layout_comment_bottom_parent);
            holder.layoutBtn = (LinearLayout) convertView
                    .findViewById(R.id.lay_btn_hiden_bottom_repeat);
            holder.tvHideBtn = (TextView) convertView
                    .findViewById(R.id.tv_hiden_bottom_repeat);
            holder.ciuserimg = (CircularImage) convertView.findViewById(R.id.ci_comment_bottom_userimg);
            holder.tvpostdata = (TextView) convertView.findViewById(R.id.tv_comment_bottom_postdate);
            holder.ivbguserimg = (ImageView) convertView.findViewById(R.id.iv_comment_bottom_bgimg);
            convertView.setTag(holder);
        } else {
            holder = (CommentViewHolder) convertView.getTag();
        }
        if (item.isBottomFloor == true) {
            holder.tvpostdata.setVisibility(View.VISIBLE);
            holder.ciuserimg.setVisibility(View.VISIBLE);
            holder.ivbguserimg.setVisibility(View.VISIBLE);
            LogUtils.d("out====>" + position);
            if (commentObj.getItem(position) != null) {
                LogUtils.d("in====>" + position);
                ImageLoader.ImageListener listener = imageLoader.getImageListener(holder.ciuserimg, R.mipmap.ic_launcher, R.mipmap.ic_launcher);
                imageLoader.get(url, listener);
            }
        } else {
            holder.tvpostdata.setVisibility(View.GONE);
            holder.ciuserimg.setVisibility(View.GONE);
            holder.ivbguserimg.setVisibility(View.GONE);
        }
        holder.tvpostdata.setText(item.time);
        holder.tvNickname.setText(item.author);
        holder.tvNum.setText("#" + item.floor);
        holder.tvContent.loadDataWithBaseURL(null, item.content, "text/html", "utf-8", null);
        LogUtils.d(item.content);
        int[] floorSumNum = commentObj.getFloorInfoByIndex(position);
        MLog.d("格式化楼层样式:position:" + position + ";num:" + floorSumNum[1]
                + ";sum:" + floorSumNum[0] + "");
        CommentFloor curFloor = null;
        if (floorSumNum[3] > 0) {
            curFloor = commentObj.getFloorByIndex(position);
        }
        CommentStyleUtils.setCommentStyleByNum(mContext, holder, curFloor, hideBtnOnclickMap, floorSumNum[0], floorSumNum[1], floorSumNum[2], floorSumNum[3]);
        return convertView;
    }

    public void refresh(Map<CommentFloor, OnClickListener> hideBtnOnclickMap, CommentObj commentObj) {
        this.hideBtnOnclickMap = hideBtnOnclickMap;
        this.commentObj = commentObj;
        notifyDataSetChanged();
    }
}
